﻿#region License
//  Distrubted Under the GNU Public License version 2 (GPLv2)
// ========================================
// 
//  QuizzMasterStudio Version 3 
//  Copyright © 2012 , The Sparks (Autism App Jam 2014)
//  All rights reserved.  
// 
// Robert Long, Zephyr Yao, Hillary Chansavang, Li Xiao, Arial Xiao
// 
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//  
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
//  The full license is also included in the root folder.
// 
// Robert Long
//  Email: rbtLong@live.com
#endregion

using System;
using System.Linq;
using Core.ScoreSystem;
using Core.User;

namespace Core.Features.Journal.ViewModel
{
    internal class JournalScorer
    {
        private readonly UserProfile _profile;
        private readonly JournalEntry _entry;

        public JournalScorer(JournalEntry entry, UserProfile profile)
        {
            _profile = profile;
            _entry = entry;
        }

        internal ScoreEntry CalculateScore()
        {
            int score = 0;

            score += firstJournalOfDayBounus();
            score += wordCountBonus();

            return new ScoreEntry("Journal Entry", score, _entry.Id);
        }

            private int wordCountBonus()
            {
                int score = 0;
                var words = _entry.Entry.Trim().Replace("  ", "").Split(' ').Length;
                if (words >= 1) score += 45;
                if (words >= 5) score += 35;
                if (words >= 10) score += 26;
                if (words >= 15) score += 21;
                if (words >= 24) score += 15;
                if (words >= 50) score += 10;
                if (words >= 100) score += 100;
                if (words >= 150) 
                    for (int i = 0; i < words-100; i++)
                        score += 2;
                return score;
            }

            private int firstJournalOfDayBounus()
            {
                int score = 0;
                if (!hasWrittenJournalToday(_profile))
                {
                    score += 500;
                    if (_entry.Entry.Length > 0)
                        score += 45;
                    if (_entry.Mood == Moods.Sad)
                        score += 35;
                }
                return score;
            }

                private bool hasWrittenJournalToday(UserProfile p)
                {
                    return p.Journal.Any(o => o.Date.Day == DateTime.Now.Day
                                              && o.Date.Month == DateTime.Now.Month
                                              && o.Date.Year == DateTime.Now.Year);
                }
    }
}
